package com.hdoit.ares.web.acc.wechat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.RedEnvelopeRecord;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IRedEnvelopeRecordService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.enums.acc.MoneyType;
import com.hdoit.ares.selfcommon.enums.acc.RedEnvelopeStatusType;
import com.hdoit.ares.selfcommon.enums.acc.State;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.RandomUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value = "/wechat/acc/redEnvelope/")
public class RedEnvelopeWechat extends BaseWechat{

	@Override
	public String getRoutePath() {	
		return "areas/wechat/acc/redEnvelope/";
	}

	private ICustomerService customerService;
	private IRedEnvelopeRecordService redEnvelopeRecordService;
	private IAccountInfoService accountInfoService;
	private IAccountDetailService accountDetailService;
	
	
	public IAccountDetailService getAccountDetailService() {
		return accountDetailService;
	}

	@Resource
	public void setAccountDetailService(IAccountDetailService accountDetailService) {
		this.accountDetailService = accountDetailService;
	}

	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}

	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	public IRedEnvelopeRecordService getRedEnvelopeRecordService() {
		return redEnvelopeRecordService;
	}

	@Resource
	public void setRedEnvelopeRecordService(
			IRedEnvelopeRecordService redEnvelopeRecordService) {
		this.redEnvelopeRecordService = redEnvelopeRecordService;
	}

	public ICustomerService getCustomerService() {
		return customerService;
	}

	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	private final String redEnvelopeIndexViewName = "redEnvelopeIndex";
	private final String redEnvelopeDetailViewName = "redEnvelopeDetail";

	
	
	@RequestMapping(value=redEnvelopeIndexViewName,method={RequestMethod.POST,RequestMethod.GET})
	public String redEnvelopeIndex(Model model,String id){
		RedEnvelopeRecord redEnvelopeRecord = redEnvelopeRecordService.queryModel(id);
		String statusString = redEnvelopeRecord.getRedenvelopeStatus();
		if(RedEnvelopeStatusType.alreadyReceived.equals(statusString)){
			System.out.println("+++++++++++++++++++++++++++++++++++++++++++已领取");
			model.addAttribute("redEnvelopeRecord",redEnvelopeRecord);
			return view(redEnvelopeDetailViewName);
		}
		if(redEnvelopeRecord!=null && ((new Date().getTime() - redEnvelopeRecord.getSendDate().getTime())>(redEnvelopeRecord.getDuration()*1000))){
			System.out.println("++++++++++++++++++++++++++++++++===过期");
			model.addAttribute("r", "s");
			return view(redEnvelopeIndexViewName);
		}
	
		if(!StringUtils.isEmpty(id)){
			model.addAttribute("r", id);
		}else{
			model.addAttribute("r", "x");
		}
		
		return view(redEnvelopeIndexViewName);
	}
	
	
	@RequestMapping(value=redEnvelopeDetailViewName,method={RequestMethod.POST,RequestMethod.GET})
	public String redEnvelopeDetail(Model model,String r){
		RedEnvelopeRecord redEnvelopeRecord = redEnvelopeRecordService.queryModel(r);
		if(redEnvelopeRecord!=null && redEnvelopeRecord.getRedenvelopeStatus().equals(RedEnvelopeStatusType.notReceived)){
			WechatLoginModel loginModel = wechatSession.getCurrentLogin();
			Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
				
				if(redEnvelopeRecord.getReceiverId().equals(customer.getId())){
					redEnvelopeRecord.setGetDate(DateUtils.getCurrent());
					redEnvelopeRecord.setRedenvelopeStatus(RedEnvelopeStatusType.alreadyReceived);
					redEnvelopeRecord.setModelStatus(ModelStatus.update);
					
					if(redEnvelopeRecordService.applyChange(redEnvelopeRecord)){
						AccountInfo custAccountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
						
							if(custAccountInfo!=null){
									//得到账户现有余额
									double balance = custAccountInfo.getBalance();
									double redEnvelMoney = redEnvelopeRecord.getRedMoney();
									balance = balance + redEnvelMoney;
									custAccountInfo.setBalance(balance);
									custAccountInfo.setModelStatus(ModelStatus.update);
									
									//在账户的余额中加钱,如果成功了，则在余额明细插入一条数据
									if(accountInfoService.applyChange(custAccountInfo)){
									
										//在余额明细插入一条数据
										AccountDetail accountDetail = new AccountDetail();				
										accountDetail.setId(CommonUtils.getKeyId());
										accountDetail.setCustomerId(customer.getId());
										accountDetail.setAccountNo(custAccountInfo.getAccountNo());
										accountDetail.setMoneyType(MoneyType.redEnvelopes);
										accountDetail.setState(State.income);
										accountDetail.setServiceId("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
										accountDetail.setTime(DateUtils.getCurrent());
										accountDetail.setMoney(redEnvelMoney);
										accountDetail.setBalance(balance);
										accountDetail.setModelStatus(ModelStatus.add);
										
										if(accountDetailService.applyChange(accountDetail)){
											model.addAttribute("redEnvelopeRecord", redEnvelopeRecordService.queryModel(r));
										}
									}
									
								}
					}
				}
		}
			model.addAttribute("redEnvelopeRecord", redEnvelopeRecord);
			return view(redEnvelopeDetailViewName);
		
	}
}
